package com.merelyb.controller;

import com.merelyb.business.service.account.BusinessAccountInfoService;
import com.merelyb.data.service.service.common.Const;
import com.merelyb.data.service.service.mybatis.bean.account.AccountInfo;
import com.merelyb.data.service.service.mybatis.bean.token.TokenInfo;
import com.merelyb.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LiM on 2017/12/29.
 */
@Controller
@RequestMapping("account")
public class accountController {
    @Autowired
    BusinessAccountInfoService businessAccountInfoService;

    /**
     * 登录验证
     * @param request
     */
    @RequestMapping(method = RequestMethod.POST, value = "login.do")
    public void checkLogin(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> jsonMap = new HashMap<>();
        String sAccout = request.getParameter("sAccount") != null? request.getParameter("sAccount"):"";
        String sPwd = request.getParameter("sPwd") != null? request.getParameter("sPwd"):"";
        if(sAccout.equals("") || sPwd.equals("")){
            jsonMap.put(Const.S_IS_SUCCESSED, false);
            jsonMap.put(Const.S_MSG, "帐号、密码不为空");
        }
        TokenInfo tokenInfo = businessAccountInfoService.checkAccount(sAccout, sPwd);
        if(tokenInfo == null){
            jsonMap.put(Const.S_IS_SUCCESSED, false);
            jsonMap.put(Const.S_MSG, "帐号、密码错误");
        }else {
            AccountInfo accountInfo = businessAccountInfoService.getAccountInfo(sAccout);
            String sAcc = accountInfo.getUserName() != null? accountInfo.getUserName():accountInfo.getTelNum()!=null?String.valueOf(accountInfo.getTelNum()):accountInfo.getEmail();
            jsonMap.put(Const.S_IS_SUCCESSED, true);
            jsonMap.put(Const.S_DATA, tokenInfo);
            jsonMap.put(Const.S_MSG, sAcc);
        }
        try {
            response.getWriter().write(JsonUtils.obj2Json(jsonMap));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 注册用户
     * @param request
     * @param response
     */
    @RequestMapping(method = RequestMethod.POST, value = "regist.do")
    public void registUser(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> jsonMap = new HashMap<>();
        String sAccount = request.getParameter("sAccount") != null? request.getParameter("sAccount"):"";
        String sPwd = request.getParameter("sPwd") != null? request.getParameter("sPwd"):"";
        String sRPwd = request.getParameter("sRPwd") != null? request.getParameter("sRPwd"): "";
        if(sAccount.equals("")){
            jsonMap.put(Const.S_IS_SUCCESSED, false);
            jsonMap.put(Const.S_MSG, "帐号不为空");
        }
        if(sPwd.equals("")){
            jsonMap.put(Const.S_IS_SUCCESSED, false);
            jsonMap.put(Const.S_MSG, "密码不为空");
        }
        if(!sPwd.equals(sRPwd)){
            jsonMap.put(Const.S_IS_SUCCESSED, false);
            jsonMap.put(Const.S_MSG, "两次输入密码不一致");
        }

        int iStatus = businessAccountInfoService.addNewAccountInfo(sAccount, sPwd);
        switch (iStatus){
            case 0:{
                jsonMap.put(Const.S_IS_SUCCESSED, true);
                jsonMap.put(Const.S_MSG, "新增用户成功");
                break;
            }
            case 1:{
                jsonMap.put(Const.S_IS_SUCCESSED, false);
                jsonMap.put(Const.S_MSG, "用户名已存在");
                break;
            }
            case -1:{
                jsonMap.put(Const.S_IS_SUCCESSED, false);
                jsonMap.put(Const.S_MSG, "系统错误，请联系管理员");
                break;
            }
        }
        try {
            response.getWriter().write(JsonUtils.obj2Json(jsonMap));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
